﻿using System;
using System.Collections.Generic;
using Sirenix.OdinInspector;
using UnityEditor;
using UnityEngine;

namespace IQIGame.Onigao.GamePlay.Editors
{
    [CreateAssetMenu(fileName = "LevelServiceEditorSetting", menuName = "Create Scriptable Config/关卡编辑器/服务编辑器设置")]
    public class LevelServiceEditorSetting : SerializedScriptableObject
    {
        public Dictionary<string, LevelServiceEditorConfig> setting = new Dictionary<string, LevelServiceEditorConfig>();

        [Flags]
        public enum ELevelServiceEditorConfigType
        {
            CustomInt = 1,
            TableID = 1 << 1,
            PickCoordinate = 1 << 5,
            CustomStr = 1 << 6,
            GraphToServiceGroup = 1 << 7,
            ServiceToUnitIndex = 1 << 8,
            InputStr = 1 << 9,
            ExecutionGroup = 1 << 10,
            InputInt = 1 << 11,
        }
    }

    [Serializable]
    public class LevelServiceEditorConfig
    {
        public LevelServiceEditorSetting.ELevelServiceEditorConfigType type;
        [ShowIf(nameof(IsShowCustomInt))] public Dictionary<int, string> value = new Dictionary<int, string>();
        [ShowIf(nameof(IsShowCustomStr))] public List<string> valueStr = new List<string>();
        [ShowIf(nameof(IsShowTableIDWithSelf))]
        public string tableName;
        [ShowIf(nameof(IsShowTableIDWithSelf))]
        public string tableProperty;
        
        private bool IsShowCustomInt()
        {
            return (type & LevelServiceEditorSetting.ELevelServiceEditorConfigType.CustomInt) > 0;
        }

        private bool IsShowCustomStr()
        {
            var b = (type & LevelServiceEditorSetting.ELevelServiceEditorConfigType.CustomStr) > 0;
            if (b && this.valueStr == null)
            {
                this.valueStr = new List<string>();
            }
            return b;
        }

        private bool IsShowTableIDWithSelf()
        {
            return (type & LevelServiceEditorSetting.ELevelServiceEditorConfigType.TableID) > 0;
        }

        private bool IsShowPosList()
        {
            return (type & LevelServiceEditorSetting.ELevelServiceEditorConfigType.PickCoordinate) > 0;
        }
    }

    public static class LevelServiceEditorSettingGlobal
    {
        public static LevelServiceEditorSetting setting;

        public static LevelServiceEditorConfig GetConfig(string key)
        {
            if (setting == null)
            {
                setting = AssetDatabase.LoadAssetAtPath<LevelServiceEditorSetting>("Assets/EditorConfig/LevelConfig/LevelServiceEditorSetting.asset");
            }
            if (setting != null && setting.setting.TryGetValue(key, out var rConfig))
            {
                return rConfig;
            }
            return null;
        }
    }
}